package com.github.pig.admin.actlistener;

import com.github.pig.admin.model.ActAssignee;
import com.github.pig.admin.service.ActAssigneeService;
import com.github.pig.admin.service.impl.ActAssigneeServiceImpl;
import com.github.pig.admin.util.AssigneeType;
import com.github.pig.admin.util.SpringUtil;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

import java.util.List;

/**
 * 
 * 流程监听器 动态注入节点办理人
 */
public class ActNodeListener implements TaskListener {

	private static final long serialVersionUID = 1L;
	
	@Override
	public void notify(DelegateTask delegateTask) {
	   String nodeId=delegateTask.getTaskDefinitionKey();//key
	   ActAssigneeService actAssigneeService= SpringUtil.getBean(ActAssigneeServiceImpl.class);
	   List<ActAssignee> assigneeList=actAssigneeService.selectList(new ActAssignee(nodeId));
	   for(ActAssignee assignee:assigneeList){
		  switch (assignee.getAssigneeType()){
		     case AssigneeType.GROUP_TYPE:
		      delegateTask.addCandidateGroup(assignee.getRoleId());
		      break;
		     case AssigneeType.USER_TYPE:
		      delegateTask.addCandidateUser(assignee.getAssignee());
		      break;
	      }
	   }
    }
}
